package com.genreate;

import com.code.CodeWriteFactory;
import com.common.config.ProjectConfig;
import com.sqlsession.SqlInfo;
import com.common.entity.CClass;
import com.excel.ExcelGenerator;
import com.sqlsession.database.CColumn;
import com.sqlsession.CSqlSession;
import com.sqlsession.database.CTable;
import com.sqlsession.database.DbType;
import com.sqlsession.filter.DbFilter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class GenerateCode {

    static DbFilter dbFilter1 = new DbFilter() {
        @Override
        public boolean filterTable(String tableName) {
            String upperCase = tableName.toUpperCase();
            List<String> ignores = new ArrayList<>();
            ignores.add("wy_simple_file".toUpperCase());
            ignores.add("wy_dkHouseStock".toUpperCase());
            ignores.add("wy_Sfkfs".toUpperCase());
            return upperCase.startsWith("wy".toUpperCase()) && !ignores.contains(upperCase);
        }
    };
    static DbFilter dbFilter2 = new DbFilter() {
        @Override
        public boolean filterTable(String tableName) {
            String upperCase = tableName.toUpperCase();
            List<String> ignores = new ArrayList<>();
            ignores.add("group1".toUpperCase());
            ignores.add("menu2".toUpperCase());
            return !ignores.contains(upperCase);
        }
    };

    static DbFilter dbFilter3 = new DbFilter() {
        @Override
        public boolean filterTable(String tableName) {
            String upperCase = tableName.toUpperCase();
            return upperCase.startsWith("app_".toUpperCase());
        }
    };

    public static void main(String[] args) throws Exception {
        GenerateCode generateCode = new GenerateCode();
        SqlInfo sqlInfo = SqlInfo.SqlInfoBuilder.aSqlInfo()
                .host("localhost")
                .port("3306")
                .databaseName("curriculum")
                .userName("root")
                .password("zhangqing")
                .dbType(DbType.MYSQL)
                .desc("小麦课堂")
                .build();
        generateCode.processSqlToClass(sqlInfo);
        //generateCode.processSqlToExcel(sqlInfo1, sqlInfo2);
    }

    public void processSqlToClass(SqlInfo... sqlInfos) throws Exception {
        CSqlSession cSqlsession = CSqlSession.openDefaultCSqlSession(sqlInfos);
        List<CTable> tables = cSqlsession.getTables();
        for (CTable table : tables) {
            List<CColumn> columns = cSqlsession.getColumns(table);
            table.setcColumns(columns);
        }
        List<CClass> cClasses = new LinkedList<>();
        for (CTable table : tables) {
            CClass cClass = CTableToClass.getClassByCTable(table);
            cClasses.add(cClass);
        }
        CodeWriteFactory.write(cClasses, projectConfig());
        cSqlsession.close();
    }

    public void processSqlToExcel(SqlInfo... sqlInfo) throws Exception {
        CSqlSession cSqlsession = CSqlSession.openDefaultCSqlSession(sqlInfo);
        List<CTable> tables = cSqlsession.getTables();
        for (CTable table : tables) {
            List<CColumn> columns = cSqlsession.getColumns(table);
            table.setcColumns(columns);
        }
        String path = System.getProperty("user.dir");
        ExcelGenerator.TablesWriteToFile(path, "xqadmin.xls", tables);
        cSqlsession.close();
    }

    protected ProjectConfig projectConfig() {
        ProjectConfig.ProjectConfigBuilder projectConfigBuilder = ProjectConfig.ProjectConfigBuilder.aProjectConfig();
        projectConfigBuilder.projectName("Test");
        projectConfigBuilder.projectPath("/Users/zhangqing/IdeaProjects");
        projectConfigBuilder.packageName("com.server.test");
        return projectConfigBuilder.build();
    }
}
